/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JSimpleObjectPanel.java
 *
 * Created on 2010-maj-18, 10:28:11
 */

package foalvleditor.gui;

import foalvleditor.core.impl.LevelModel;
import foalvleditor.core.impl.SelectionModel;
import foalvleditor.gameobjects.AbstractGameObject;
import java.awt.Color;
import java.awt.Image;
import javax.swing.ImageIcon;

/**
 *
 * @author Mikael
 */
public class JSimpleObjectPanel extends javax.swing.JPanel {

    /**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private AbstractGameObject ago;
    private static final LevelModel levelModel=LevelModel.getInstance();
    private static final SelectionModel selectionModel=SelectionModel.getInstance();
    private boolean hardSelection=false;

    /** Creates new form JSimpleObjectPanel */
    public JSimpleObjectPanel() {
        super();
        initComponents();
    }

    public JSimpleObjectPanel(AbstractGameObject goa) {
        this();
        setObject(goa);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        iconButton = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(48, 48));
        setMinimumSize(new java.awt.Dimension(48, 48));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(48, 48));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(foalvleditor.core.FoaLvlEditorApp.class).getContext().getResourceMap(JSimpleObjectPanel.class);
        iconButton.setText(resourceMap.getString("iconButton.text")); // NOI18N
        iconButton.setContentAreaFilled(false);
        iconButton.setFocusPainted(false);
        iconButton.setMaximumSize(new java.awt.Dimension(48, 48));
        iconButton.setMinimumSize(new java.awt.Dimension(48, 48));
        iconButton.setName("iconButton"); // NOI18N
        iconButton.setPreferredSize(new java.awt.Dimension(48, 48));
        iconButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                iconButtonMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                iconButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                iconButtonMouseExited(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(iconButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(iconButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void iconButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_iconButtonMouseEntered
        if (!hardSelection)
            selectObject();
    }//GEN-LAST:event_iconButtonMouseEntered

    private void iconButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_iconButtonMouseExited
        if (!hardSelection)
            deselectObject();
    }//GEN-LAST:event_iconButtonMouseExited

    private void iconButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_iconButtonMouseClicked
    if(hardSelection) deselectObject();
    else hardSelectObject();
    }//GEN-LAST:event_iconButtonMouseClicked

    public final void setObject(AbstractGameObject goa) {
        this.ago=goa;
        Image img = goa.getBitmap().getBufferedImage();
        this.iconButton.setIcon(new ImageIcon(img));
        this.repaint();
    }


    public void hardSelectObject(){
        {
            this.setBackground(Color.GRAY);
            this.hardSelection=true;
            this.repaint();
            if (!selectionModel.isSelected(ago))
                selectionModel.select(ago);
            levelModel.getView().hardSelectPanel(ago);
            levelModel.getView().repaintAll();}
    }

    public void selectObject(){
        this.setBackground(Color.LIGHT_GRAY);
        this.repaint();
        if (!selectionModel.isSelected(ago)) selectionModel.select(ago);
        levelModel.getView().repaintPanel(ago);
        levelModel.getView().repaintAll();
    }

    public void deselectObject(){
        this.setBackground(new Color(0xE0DFE3));
        this.hardSelection=false;
        this.repaint();
        selectionModel.deselect(ago);
        levelModel.getView().repaintPanel(ago);
        levelModel.getView().repaintAll();
    }

    public AbstractGameObject getObject(){
        return this.ago;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton iconButton;
    // End of variables declaration//GEN-END:variables

}
